package com.haojin.spring.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;
import org.springframework.validation.Validator;

/**
 * @author .29.
 * @create 2023-03-02 9:24
 */
//使用spring提供的validate校验方法
@Service
public class MyService2 {
    @Autowired
    private Validator validator;

    public boolean validator2(User user){
        BindException bindException = new BindException(user,user.getName());
        validator.validate(user,bindException);            //调用校验方法进行校验
        System.out.println(bindException.getAllErrors());  //输出所有错误信息
        return bindException.hasErrors();                  //若没有异常，返回false

    }

}
